package com.yb.livewykt.util

import android.content.SharedPreferences
import com.yb.livewykt.LiveApplication

class SaveUserData {

    companion object{
        private lateinit var sharedPreferences: SharedPreferences
        private lateinit var editor: SharedPreferences.Editor

        private var instance:SaveUserData? = null
        get() {
            if (field == null){
                field = SaveUserData()
            }
            return field
        }
        fun get():SaveUserData {
            sharedPreferences = LiveApplication.context.getSharedPreferences("user",0)
            editor = sharedPreferences.edit()
            return instance!!
        }
    }

    var token:String
    set(value) {
        editor.apply {
            putString("token",value)
            apply()
        }
    }
    get() {
        return sharedPreferences.getString("token","")!!
    }

    var accId:String
        set(value) {
            editor?.apply {
                putString("accid",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getString("accid","")!!

    var imToken:String
        set(value) {
            editor?.apply {
                putString("im_token",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getString("im_token","")!!

    var username:String
        set(value) {
            editor?.apply {
                putString("username",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getString("username","")!!

    var id:Int
        set(value) {
            editor?.apply {
                putInt("id",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getInt("id",0)!!

    var role:Int
        set(value) {
            editor?.apply {
                putInt("role",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getInt("role",0)!!

    var qrCodeUrl:String
        set(value) {
            editor?.apply {
                putString("qr_code_url",value)
                apply()
            }
        }
        get() =  sharedPreferences?.getString("qr_code_url","")!!


    var notification:Boolean
        set(value) {
            editor?.apply {
                putBoolean("is_notification",value)
                apply()
            }
        }
        get() = sharedPreferences?.getBoolean("is_notification",false)

    var isNeedUpdateDB:Boolean
        set(value){
            editor?.apply {
                putBoolean("need_update",value)
            }
        }
        get() = sharedPreferences?.getBoolean("need_update",true)



    var uuid:String
        set(value) {
            editor?.apply {
                putString("uuid",value)
                apply()
            }
        }
        get() = sharedPreferences?.getString("uuid","1111113131231")!!


    var phone:String
        set(value) {
            editor?.apply {
                putString("phone",value)
                apply()
            }
        }
        get() = sharedPreferences?.getString("phone","")!!

    var password:String
        set(value) {
            editor?.apply {
                putString("password",value)
                apply()
            }
        }
        get() = sharedPreferences?.getString("password","")!!


    fun clearData(){
        editor?.apply{
            clear()
            apply()
        }
    }
}